package org.myorg.quickstart;

public class LDPacket {

    int  frameSeq;    //2 byte
    int  packetSeq;   //2 byte
    int  packetLen;   //2 byte
    byte[]  reserved;    //2 byte
    byte[]  data;        //1460 byte
    byte[]  checksum;    //4 byte

    public LDPacket(int frameSeq, int packetSeq, int packetLen, byte[] reserved, byte[] data, byte[] checksum) {
        this.frameSeq = frameSeq;
        this.packetSeq = packetSeq;
        this.packetLen = packetLen;
        System.arraycopy(reserved, 0, this.reserved, 0, 2);
        System.arraycopy(data, 0, this.data, 0, 1460);
        System.arraycopy(checksum, 0, this.checksum, 0, 4);
    }

    public LDPacket(byte[] packetRaw) {
        //System.arraycopy(packetRaw, 0, this.frameSeq, 0, 2);

       // System.arraycopy(packetRaw, 2, this.packetSeq, 0, 2);

        //System.arraycopy(packetRaw, 4, this.packetLen, 0, 2);
        //this.frameSeq = Utils.twobytes2Int(packetRaw);

        this.frameSeq = (packetRaw[0]&0xff) | (packetRaw[1]&0xff)<<8;
        this.packetSeq = (packetRaw[2]&0xff) | (packetRaw[3]&0xff)<<8;
        this.packetLen = (packetRaw[4]&0xff) | (packetRaw[5]&0xff)<<8;

        System.arraycopy(packetRaw, 6, this.reserved, 0, 2);
        System.arraycopy(packetRaw, 8, this.data, 0, 1460);
        System.arraycopy(packetRaw, 1468, this.checksum, 0, 4);
    }

    public int getFrameSeq() {
        return frameSeq;
    }

    public void setFrameSeq(int frameSeq) {
        this.frameSeq = frameSeq;
    }

    public int getPacketSeq() {
        return packetSeq;
    }

    public void setPacketSeq(int packetSeq) {
        this.packetSeq = packetSeq;
    }

    public int getPacketLen() {
        return packetLen;
    }

    public void setPacketLen(int packetLen) {
        System.arraycopy(reserved, 0, this.reserved, 0, 2);
    }

    public byte[] getReserved() {
        return reserved;
    }

    public void setReserved(byte[] reserved) {
        this.reserved = reserved;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        System.arraycopy(data, 0, this.data, 0, 1460);
    }

    public byte[] getChecksum() {
        return checksum;
    }

    public void setChecksum(byte[] checksum) {
        System.arraycopy(checksum, 0, this.checksum, 0, 4);
    }

    public byte[] getBytes() {

        byte[] value = new byte[2*4+1460+4];

        value[0] = (byte)((this.frameSeq >> 8) & 0xFF);
        value[1] = (byte)(this.frameSeq & 0xFF);

        value[2] = (byte)((this.packetSeq >> 8) & 0xFF);
        value[3] = (byte)(this.packetSeq & 0xFF);

        value[4] = (byte)((this.packetLen >> 8) & 0xFF);
        value[5] = (byte)(this.packetLen & 0xFF);

        System.arraycopy(this.reserved, 0, value, 2*3, 2);
        System.arraycopy(this.data, 0, value, 2*3 + 2 , 1460);
        System.arraycopy(this.checksum, 0, value, 2*3 + 2 + 1460, 4);

        return value;
    }

}
